/*
 * ﻿Copyright (C) 2013-2014 NewMain Softech
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.newmainsoftech.eatestutil.container.glassfish;

import java.text.MessageFormat;
import java.util.logging.Handler;
import java.util.logging.Level;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.newmainsoftech.eatestutil.container.glassfish.Slf4jJulLoggerConfigurator;

public class GlassfishDaoLoggerConfigurator {
	public static enum GlassfishDaoLoggerEnum {
		/** org.glassfish.ejb
		 */
		gfEjb( "org.glassfish.ejb"), 
		/** org.glassfish.persistence
		 */
		gfPersistence( "org.glassfish.persistence"),
		/** org.glassfish.kernel.embedded
		 */
		gfEmbedded( "org.glassfish.kernel.embedded"),
		/** org.eclipse.persistence
		 */
		ecPersistence( "org.eclipse.persistence"),
		/** javax.org.glassfish.persistence
		 */
		jgfPersistence( "javax.org.glassfish.persistence"),
		/** javax.enterprise.resource.jta
		 */
		jta( "javax.enterprise.resource.jta"),
		/** javax.enterprise.system.core.naming
		 */
		jeNaming( "javax.enterprise.system.core.naming"),
		/** javax.enterprise.system.core.transaction
		 */
		jeTransaction( "javax.enterprise.system.core.transaction"),
		/** javax.enterprise.system.container.ejb
		 */
		jeEjb( "javax.enterprise.system.container.ejb"),
		/** javax.enterprise.system.core.classloading
		 */
		jeClassloading( "javax.enterprise.system.core.classloading"),
		/** com.sun.ejb.containers
		 */
		ejbContainers( "com.sun.ejb.containers");
		
		private final String loggerName;
			public String getLoggerName() {
				return loggerName;
			}
		private Level level;
			public Level getLevel() {
				return level;
			}
			public void setLevel( final Level level) {
				this.level = level;
			}

		private GlassfishDaoLoggerEnum( final String loggerName) {
			this.loggerName = loggerName;
		}
	}
	
	public void setLoggersLevel( final GlassfishDaoLoggerEnum glassfishDaoLoggerEnum) {
		if ( glassfishDaoLoggerEnum == null) {
			throw new IllegalArgumentException(
					MessageFormat.format(
							"Value of {0} argument must be a {1} enum instance but null.",
							"glassfishDaoLoggerEnum", 
							GlassfishDaoLoggerEnum.class.getSimpleName())
					);
		}
		if ( glassfishDaoLoggerEnum.getLevel() == null) {
			throw new IllegalArgumentException(
					MessageFormat.format(
							"{0} value has not been set in {1} given by {2} argument.",
							Level.class.getSimpleName(),
							glassfishDaoLoggerEnum, 
							"glassfishDaoLoggerEnum")
					);
		}
		
		final Level level = glassfishDaoLoggerEnum.getLevel();
		
		final java.util.logging.Logger globalLogger = java.util.logging.Logger.getGlobal();
			final Level globalLoggerLevel = globalLogger.getLevel();
			if ( !Level.class.isInstance( globalLoggerLevel) 
					|| ( level.intValue() < globalLoggerLevel.intValue())) {
				globalLogger.setLevel( level);
			}
			
			for( Handler handler : globalLogger.getHandlers()) {
				Level handlerLevel = handler.getLevel();
				if ( !Level.class.isInstance( handlerLevel) 
						|| ( level.intValue() < handlerLevel.intValue())) {
					handler.setLevel( level);
				}
			}
			
			
		java.util.logging.Logger logger = null;
		switch( glassfishDaoLoggerEnum) {
		case jeEjb:
			{
				logger = java.util.logging.Logger.getLogger( glassfishDaoLoggerEnum.getLoggerName());
			}
			break;
		default:
			{
				final Slf4jJulLoggerConfigurator slf4jJulLoggerConfigurator 
				= new Slf4jJulLoggerConfigurator();
				if ( slf4jJulLoggerConfigurator.isBoundToJDK14LoggerAdapter()) {
					Logger rootSlf4jLogger = LoggerFactory.getLogger( Logger.ROOT_LOGGER_NAME);
					logger = slf4jJulLoggerConfigurator.getBoundJulLogger( rootSlf4jLogger);
					for( Handler handler : globalLogger.getHandlers()) {
						Level handlerLevel = handler.getLevel();
						if ( !Level.class.isInstance( handlerLevel) 
								|| ( level.intValue() < handlerLevel.intValue())) {
							handler.setLevel( level);
						}
					}
					
					Logger slf4jLogger = LoggerFactory.getLogger( glassfishDaoLoggerEnum.getLoggerName());
					logger = slf4jJulLoggerConfigurator.getBoundJulLogger( slf4jLogger);
				}
			}
			break;
		}
		if ( logger != null) {
			logger.setLevel( level);
			
			for( Handler handler : globalLogger.getHandlers()) {
				Level handlerLevel = handler.getLevel();
				if ( !Level.class.isInstance( handlerLevel) 
						|| ( level.intValue() < handlerLevel.intValue())) {
					handler.setLevel( level);
				}
			}
		}
	}
}
